from django.db import models
from control.models.User import User
import uuid
from .Keyword import Keyword
from .Institution import Institution
from .Department import Department
from .Author import Author
from .GlobalTables import JournalPublicationCycleType, ConferenceMeetingCycleType, Language

# 出版物信息
class Publication(models.Model): 
    uuid = models.UUIDField(primary_key = True,auto_created = True,default = uuid.uuid4,editable = False)
    name = models.CharField(verbose_name="名称", max_length=512)
    zh_name = models.CharField(verbose_name="中文名", max_length=512, null=True, blank=True)
    abbreviation = models.CharField(verbose_name="简称缩写", max_length=64, null=True, blank=True)
    website = models.URLField(verbose_name="官方网站", max_length=256, null=True, blank=True)
    description = models.TextField(verbose_name="简介", null=True, blank=True)	
    
    creator = models.ForeignKey(User, verbose_name="创建者", related_name="pub_creator", on_delete=models.CASCADE)				
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    last_editor = models.ForeignKey(User, verbose_name="最后编辑者", related_name="pub_last_editor", on_delete=models.CASCADE)	
    last_edit_date = models.DateTimeField(verbose_name='最后编辑时间', auto_now=True)					
    reviewer = models.ForeignKey(User, verbose_name="审核者", related_name="pub_reviewer", on_delete=models.CASCADE, null=True)
    review_date = models.DateTimeField(null=True, blank=True, verbose_name='审核时间')
    
    is_reviewing = models.BooleanField(verbose_name="审核状态", default=False)
    
    subjects = models.ManyToManyField(Keyword, verbose_name="研究领域主题")	
    institutions = models.ManyToManyField(Institution, verbose_name="所属机构")			
    departments = models.ManyToManyField(Department, verbose_name="所属部门")	

    class Meta:
        ordering = ['-name', '-zh_name', '-abbreviation']
        verbose_name = "Publication"           # 单数名称
        verbose_name_plural = "Publications"   # 复数名称
        indexes = [
            models.Index(fields=['name', 'zh_name'], name='final_query_idx_publication')
        ]

    def __str__(self):
        return self.name


# 书籍信息
class Book(Publication):
    publish_date = models.DateField(verbose_name="出版日期", null=True, blank=True)		      
    isbn = models.CharField(verbose_name="国际标准书号", max_length=200, null=True, blank=True)
    page_number = models.PositiveIntegerField(verbose_name="总页码", null=True, blank=True)
    language = models.ForeignKey(Language, verbose_name="语言", on_delete=models.CASCADE)
    
    authors = models.ManyToManyField(Author, verbose_name="作者列表")		

    class Meta:
        ordering = ['-name', '-zh_name', '-isbn', 'publish_date']
        verbose_name = "书籍"           # 单数名称
        verbose_name_plural = "Books"   # 复数名称

    def __str__(self):
        return self.name


# 期刊信息
class Journal(Publication):
    issn = models.CharField(verbose_name="国际标准出版物号", max_length=128, null=True, blank=True)    
    cas = models.CharField(verbose_name="cas源代码", max_length=32, null=True, blank=True)			    
    impact_factor = models.FloatField(verbose_name="影响因子", null=True, blank=True)						
    founding_datej = models.DateField(verbose_name="创刊日期", null=True, blank=True)	
    contact_emailj = models.EmailField(verbose_name="联系邮箱", max_length=128, null=True, blank=True)
    publication_cycle = models.ForeignKey(JournalPublicationCycleType, verbose_name="出版周期", on_delete=models.CASCADE)             

    class Meta:
        ordering = ['-name', '-zh_name', '-issn', '-impact_factor', 'founding_datej']
        verbose_name = "期刊"           # 单数名称
        verbose_name_plural = "Journals"   # 复数名称

    def __str__(self):
        return self.name
    

# 会议信息
class Conference(Publication):
    contact_emailc = models.EmailField(verbose_name="联系邮箱", max_length=128, null=True, blank=True)
    founding_datec = models.DateField(verbose_name="创始日期", null=True, blank=True)
    meeting_cycle = models.ForeignKey(ConferenceMeetingCycleType, verbose_name="开会周期", on_delete=models.CASCADE)     

    class Meta:
        ordering = ['-name', '-zh_name', '-founding_datec']
        verbose_name = "会议"           # 单数名称
        verbose_name_plural = "Conferences"   # 复数名称

    def __str__(self):
        return self.name  